package com.chenhy.springbootvuesmbms.service;


import com.chenhy.springbootvuesmbms.api.DrawCaptcha;
import com.chenhy.springbootvuesmbms.pojo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class CaptchaServiceImpl implements CaptchaService {

//    @Autowired
//    private RedisTemplate redisTemplate;

    @Override
    public BufferedImage getCaptchaImg(HttpServletRequest request) {

        int width = 200;
        int height = 60; // 69

        UUID uuid = UUID.randomUUID();
        request.getSession().setAttribute("CaptchaUUID", uuid);

        BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        String verifyCode = DrawCaptcha.drawRandomCaptcha(width, height, verifyImg);

//        redisTemplate.opsForValue().set(uuid.toString(),verifyCode,5,TimeUnit.MINUTES);

        return verifyImg;
    }

    @Override
    public CommonResult verify(String uuid, String captcha) {

//        String code = (String)redisTemplate.opsForValue().get(uuid);

//        if (captcha.equals(code)){
//            return new CommonResult(200,"验证成功");
//        } else {
//            return new CommonResult(444,"验证失败");
//        }
        return new CommonResult(444,"测试中");
    }
}
